package net.kazed.nextaction.timer;

import net.kazed.nextaction.R;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * Dialog for timer.
 * @author Koert Zeilstra
 */
public class TimerDialog extends Dialog {

   private SharedPreferences preferences;
   private Spinner timerType;
   private EditText fieldDuration;
   private Notifier notifier;
   private int selectedType = 0;
   private String[] timerTypeName;
   private String[] timerDefaults;
   private int[] typeIds;
   private Uri notificationUri;

   /**
    * Constructor.
    * @param context Android context.
    */
   protected TimerDialog(Context context, String[] timerTypeName, String[] timerDefaults, int[] typeIds, Uri notificationUri) {
      super(context);
      this.timerTypeName = timerTypeName;
      this.timerDefaults = timerDefaults;
      this.typeIds = typeIds;
      this.notificationUri = notificationUri;
      preferences = PreferenceManager.getDefaultSharedPreferences(context);
      notifier = new Notifier(context);
   }

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.timer_dialog);
      setTitle(R.string.timer_dialog_title);

      timerType = (Spinner) findViewById(R.id.timer_type);
      fieldDuration = (EditText) findViewById(R.id.timer_duration);
      
      Resources resources = getContext().getResources();
      String[] typeLabel = new String[timerTypeName.length];
      for (int i = 0; i < typeIds.length; i++) {
         typeLabel[i] = resources.getString(typeIds[i]);
      }
      ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item,
            android.R.id.text1, typeLabel);
      typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

      timerType.setAdapter(typeAdapter);
      timerType.setOnItemSelectedListener(new OnItemSelectedListener() {
         public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            selectType(position);
         }
         public void onNothingSelected(AdapterView<?> parent) {
         }
      });
      Button okButton = (Button) findViewById(R.id.ok_button);
      okButton.setOnClickListener(new android.view.View.OnClickListener() {
         public void onClick(View view) {
            startTimer();
         }
      });

      Button cancelButton = (Button) findViewById(R.id.cancel_button);
      cancelButton.setOnClickListener(new android.view.View.OnClickListener() {
         public void onClick(View view) {
            dismiss();
         }
      });

      selectType(selectedType);
   }
   
   /**
    * Handles change of select type.
    * @param position Timer type selected index.
    */
   private void selectType(int position) {
      selectedType = position;
      String name = timerTypeName[position];
      String defaultValue = timerDefaults[position];
      String duration = preferences.getString(name, defaultValue);
      fieldDuration.setText(duration);
   }

   /**
    * Start the timer.
    */
   private void startTimer() {
      notifier.timerCancel(notificationUri);

      String duration = fieldDuration.getText().toString();
      if (duration.length() == 0) {
         Toast.makeText(getContext(), R.string.timer_value_invalid, 2000).show();
      } else {
         int durationMinutes = Integer.parseInt(duration);
         if (durationMinutes <= 0) {
            Toast.makeText(getContext(), R.string.timer_value_invalid, 2000).show();
         } else {
            Editor editor = preferences.edit();
            String name = timerTypeName[selectedType];
            String timerDescription = null;
            timerDescription = getDescription(selectedType);
            editor.putString(name, duration);
            editor.commit();
            notifier.timerStart(notificationUri, notificationUri, timerDescription, durationMinutes);
            dismiss();
         }
      }
   }

   protected String getDescription(int selectedType) {
      Resources resources = getContext().getResources();
      String description = resources.getString(typeIds[selectedType]);
      return description;
   }
   
}
